package com.nafets.util;

import com.nafets.LineCoef;

public class MathUtils {

	public static double getAlpha(double x1, double y1, double x2, double y2, double length) {
		double alpha = 1 - length / Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));

		return alpha;
	}

	public static double getCoordinate(double alpha, double c1, double c2) {
		double c = alpha * c1 + (1 - alpha) * c2;

		return c;
	}

	public static LineCoef getLineCoefficients(double x1, double y1, double x2, double y2) {
		double a = (y2 - y1) / (x2 - x1);
		double b = (y1 * x2 - x1 * y2) / (x2 - x1);

		return new LineCoef(a, b);
	}

	public static double area(double a, double b, double c) {
		double p = (a + b + c) / 2;

		return Math.sqrt(p * (p - a) * (p - b) * (p - c));
	}

	public static double lineLength(double x1, double y1, double x2, double y2) {
		return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
	}

	public static double getX(double a, double b, double y) {
		return (y - b) / a;
	}

	public static double distanceFromLine(double x, double y, double x1, double y1, double x2, double y2, double coefA,
			double coefB) {
		double a = lineLength(x, y, x2, y2);
		double b = lineLength(x1, y1, x, y);
		double c = lineLength(x1, y1, x2, y2);
		double area = MathUtils.area(a, b, c);
		
		return 2 * area / c;
	}
}
